<?php
namespace Mobile\Controller;

use Mobile\Controller\Base\BaseController;
use Mobile\Controller\Manager\BookManager;

class WorksController extends BaseController {
	
	public function _initialize(){
		parent::_initialize();
		$this->BookManager = new BookManager;
	}
	
	/**
	 * 女频数据处理
	 */
	public function grilData(){
		
	}
	
	/**
	 * 男频数据处理
	 */
	public function manData(){
		
	}
	
    public function read(){
		
		$path = I("path.");
		$book_id = $path[2];
		
		if( !isset($path[1]) || !isset($path[2]) || strlen($path[1]) != 32 || intval($path[2]) < 1){
			// 404
		}
		
		$book_id = $path[1];
		$chapter_id = intval( $path[2] );
		
		// 获取书籍信息信息
		$bookInfo = $this->BookManager->getBookInfo($book_id);
		
		// 是否为收费章节
		$isCharge = $this->BookManager->isChapterCharge( $bookInfo['vip_chapter'], $chapter_id );
		
		// 添加阅读记录
		if( $this->userManager->isLogin() === true ){
			$this->BookManager->addReadHistory($book_id, $chapter_id, $this->userManager->user_id);
		}
		
		// 收费 未登录
		if( $isCharge === true && $this->userManager->isLogin() === false ){
			// 跳转登录
			redirect('/mobile/passport/login');
			$url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
			session('loginSuccess_redirect', $url);
		}
		// 收费 已登录
		else if( $isCharge === true && $this->userManager->isLogin() === true ){
			
			// 检测阅读收费具有权限
			$isAlready =  $this->BookManager->isChapterAlready( $book_id, $chapter_id, $this->userManager->user_id);
			if( $isAlready === false ){
				// 跳转支付
				$this->failReadAuth($bookInfo, $chapter_id);
			}else{
				$this->succReadAuth($bookInfo, $chapter_id);
			}
		}
		// 免费
		else{
			$this->succReadAuth($bookInfo, $chapter_id);
		}

	}
	
	/**
	 * 具有阅读权限
	 */
	private function succReadAuth( $bookInfo, $chapter_id ){
		
		$code = I('code');
		!empty($code) && cookie('ADReferer',$code);
		
		$book_id = $bookInfo['book_id'];
		
		$chapterAll = $this->BookManager->getBookChapterAll( $book_id, 'cid');
		
		// 章节详情
		$chapterInfo = $this->BookManager->getBookChapterInfo( $book_id, $chapter_id );

		$chapterInfo['content'] = $this->BookManager->formatContent($chapterInfo['content']);

		$page = $this->getChapterNextPrev( $chapterAll, $chapter_id );
		
		$this->assign('fontSize', session('fontSize'));
		$this->assign('light', session('light'));
		$this->assign('bookInfo', $bookInfo);
		$this->assign('chapterInfo', $chapterInfo);
		$this->assign('page', $page);
		$this->assign('mini_title', $bookInfo['title']);
		$this->display("Works:read");
		
	}
	
	// 获取上下页
	private function getChapterNextPrev( $ids, $curr_id ){

		$prev_id = '';
		$next_id = '';
		
		$ids = array_column($ids, 'cid');

		$flag = false;
		
		foreach( $ids as $val ){

			if( $flag === false && $curr_id != $val){
				$prev_id = $val;
			}
			
			if( $flag === true ){
				$next_id = 	$val;
				break;
			}
			
			if( $curr_id == $val ){
				$flag = true;
			}
		}
		
		return ['prev_id'=>$prev_id, 'next_id'=>$next_id];
		
	}
	
	/**
	 * 没有阅读权限
	 */
	private function failReadAuth( $bookInfo, $chapter_id ){

		$book_id = $bookInfo['book_id'];
		
		// 检测是否开通自动付费
		$autoOrderState = $this->BookManager->checkBookAutoOrder($book_id, $this->userManager->user_id);

		// 余额充足开始扣费
		if( $autoOrderState === true && $this->userManager->getUserPoint($this->userManager->user_id) >= $bookInfo['price'] ){
			if( $this->userManager->userDecPoint( $this->userManager->user_id, $bookInfo['price'] ) === true ){

				$data = array(
					'user_id'=>$this->userManager->user_id, 
					'book_id'=>$book_id, 
					'chapter_id'=>$chapter_id,
					'price'	=>	$bookInfo['price'],
					'ctime'	=> time()
				);
				
				$this->BookManager->addAlready( $data, 'single' );
				$this->succReadAuth($bookInfo, $chapter_id);
				exit;
			}
			
		}
		// 余额不足 跳转收费提示
		else{
		
			// 章节详情    
			$chapterInfo = $this->BookManager->getBookChapterInfo( $book_id, $chapter_id );
			
			// 生成订单号
			$out_trade_no = md5( microtime() . $book_id . $chapter_cid );
			
			// 保存临时订单号
			session('tmpOrder', array(
				'out_trade_no'	=>	$out_trade_no,
				'book_id'		=>	$book_id,
				'chapter_id'	=>	$chapter_id,
				'chapter_price'	=>	$bookInfo['price'],
			));
			
			$this->assign( "chapter_size", $chapterInfo['size'] );
			$this->assign( "chapter_price", $bookInfo['price'] );
			$this->assign( "out_trade_no", $out_trade_no );
			$this->assign( "user_point", $this->userManager->getUserPoint($this->userManager->user_id) );
			$this->assign( "referer", base64_encode($book_id . '/' . $chapter_id) );
	
			$this->display('Works:subscription');
			
		}
		
	}
}